﻿using Business;
using Model;
using Newtonsoft.Json;
using Public;
using Public.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using wochu.DPS.API.Infrastructure;

namespace wochu.DPS.API.Controllers
{
    public class LmlocationGoodsController : ApiController
    {
        private static BLmlocationgoods _NewInstance = new BLmlocationgoods();

        public static BLmlocationgoods NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new BLmlocationgoods();
                }

                return _NewInstance;
            }


        }

        [HttpGet]
        public HttpResponseMessage GetLmlocationGoods(string locationcode)
        {
            SysSetting sysSet = new SysSetting();
            if (locationcode == null)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                bool result = _NewInstance.Lmlocationgoods_warehouse(locationcode);
                if (!result)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "该货位不属于摘果仓";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                var data = _NewInstance.List_Lmlocationgoods(locationcode);
                if (data.Count <= 0 || data == null)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无关联商品";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                if (data[0].QTY == 0)
                { 
                     sysSet.Key = Consts.strSucess;
                     sysSet.Value = "商品暂无库存";
                     return ResultsExtensions.GetFailJson(sysSet, data);
                }
                return ResultsExtensions.GetSuccessJson(data);

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        [HttpGet]
        public HttpResponseMessage GetLMlocationInfo(string LocationCode)
        {
            SysSetting sysSet = new SysSetting();
            if (LocationCode == null)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                var data = _NewInstance.GetLMLocationInfoByCode(LocationCode);
                if (data == null)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无货位信息";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                return ResultsExtensions.GetSuccessJson(data);

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }
        }

        [HttpPost]
        public HttpResponseMessage UpdateLMLocationStock(List<LMLocationModel> List)
        {
            SysSetting sysSet = new SysSetting();
            if (List == null || List.Count<=0)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数有误,请检查参数";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            else
            {
                bool _result = _NewInstance.UpdatelocationMaxStock(List);
                if (_result)
                {
                    return ResultsExtensions.GetSuccessJson(_result);
                }
                else
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "提交失败，请联系管理员";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
            }
        }

        [HttpPost]
        public HttpResponseMessage Change_locationGoods(int Change_ALOCATIONID, int Change_AGOODSID, int Change_BLOCATIONID, int Change_BGOODSID, int CREATEDAID)
        {
            string message = "";
            SysSetting sysSet = new SysSetting();
            if (Change_ALOCATIONID == 0 || Change_AGOODSID == 0 || Change_BLOCATIONID == 0 || Change_BGOODSID == 0 )
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数有误,请检查参数";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            else
            {
                bool _result = _NewInstance.PostlocationGoodslist(Change_ALOCATIONID, Change_AGOODSID, Change_BLOCATIONID, Change_BGOODSID, CREATEDAID);
                if (_result)
                {
                    return ResultsExtensions.GetSuccessJson(_result);
                }
                else
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "提交失败，请联系管理员";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
            }
        }

        #region 加工仓的电子标签和货位信息绑定
        [HttpGet]
        public HttpResponseMessage checkProduceLabelLocation(string LabelID)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(LabelID))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                produce_eleclabellocationModel result = _NewInstance.checkLabelIsbind(LabelID);
                if (result==null)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无该电子标签相关配置信息";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                if (!string.IsNullOrEmpty(result.LOCATIONCODE))
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "电子标签已绑定货位："+ result.LOCATIONCODE+",请先解除关联";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                sysSet.Key = Consts.strSucess;
                sysSet.Value = "可以绑定";
                return ResultsExtensions.GetFailJson(sysSet,"sucess");

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        [HttpPost]
        public HttpResponseMessage DebindProduceLabelLocation(string LabelID)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(LabelID))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                bool result = _NewInstance.DebindLabelLocationRelation(LabelID);
                if (!result)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "解绑失败";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                return ResultsExtensions.GetSuccessJson("sucess");

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 解绑失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        [HttpPost]
        public HttpResponseMessage bindProduceLabelLocation(int UserID, List<produce_eleclabellocationModel> list)
        {
            SysSetting sysSet = new SysSetting();
            if (UserID == 0 || list == null || list.Count <= 0)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数有误,请检查参数";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            else
            {
                bool _result = _NewInstance.bindProduceLabelLocation(UserID,list);
                if (_result)
                {
                    return ResultsExtensions.GetSuccessJson(_result);
                }
                else
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "提交失败，请联系管理员";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
            }
        }
        #endregion

        [HttpGet]
        public HttpResponseMessage GetProduceBatchInfo(string ProduceBatchNO)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(ProduceBatchNO))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                producesaleorderbatchitemModel result = _NewInstance.getProduceQtyInfo(ProduceBatchNO);
                if (result == null)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无加工分拣批次信息";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                return ResultsExtensions.GetSuccessJson(result);

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        [HttpGet]
        public HttpResponseMessage GetProducePickBatchInfo(string ProduceBatchNO)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(ProduceBatchNO))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                List<ProducePickSituation> result = _NewInstance.getProducePickInfo(ProduceBatchNO);
                if (result == null || result.Count<=0)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无加工分拣缺货信息";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                return ResultsExtensions.GetSuccessJson(result);

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }


        [HttpPost]
        public HttpResponseMessage UpdateProduceBatchInfo(producesaleorderbatchitemModel  produceInfo)
        {
            SysSetting sysSet = new SysSetting();
            if (produceInfo==null)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                bool result = _NewInstance.UpdateProducefinishQTY(produceInfo);
                if (!result)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "核验加工商品数量失败";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                return ResultsExtensions.GetSuccessJson("sucess");

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 核验失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        #region 摘果仓电子标签货位绑定
        //获取摘果仓货位关联商品信息
        [HttpGet]
        public HttpResponseMessage GetPFTLMlocationInfo(string LocationCode)
        {
            SysSetting sysSet = new SysSetting();
            if (LocationCode == null)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                var data = _NewInstance.GetPFTLMLocationInfoByCode(LocationCode);
                if (data == null)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无该货位信息";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                else if (data.WAREHOUSEID != 713)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "该货位不属于摘果仓";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                else if (!string.IsNullOrEmpty(data.IPADDRESS))
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "该货位已绑定电子标签" + data.IPADDRESS + ":" + data.LABELID + ";请先解除绑定";
                    return ResultsExtensions.GetFailJson(sysSet, new ResultJsonInfo() {Msg = data.LABELID });
                }
                else
                {
                    return ResultsExtensions.GetSuccessJson(data);
                }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }
        }
        [HttpGet]
        public HttpResponseMessage checkPftLabelLocation(string LabelID)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(LabelID))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                pft_eleclabellocationModel result = _NewInstance.checkPFTLabelIsbind(LabelID);
                if (result == null)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无该电子标签相关配置信息";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                if (!string.IsNullOrEmpty(result.LOCATIONCODE))
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "电子标签已绑定货位：" + result.LOCATIONCODE + ",请先解除关联";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                sysSet.Key = Consts.strSucess;
                sysSet.Value = "可以绑定";
                return ResultsExtensions.GetFailJson(sysSet, "sucess");

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        [HttpPost]
        public HttpResponseMessage DebindPFTLabelLocation(string LabelID)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(LabelID))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                bool result = _NewInstance.DebindPFTLabelLocationRelation(LabelID);
                if (!result)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "解绑失败";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
                return ResultsExtensions.GetSuccessJson("sucess");

            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 解绑失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        [HttpPost]
        public HttpResponseMessage bindPFTLabelLocation(int UserID, List<pft_eleclabellocationModel> list)
        {
            SysSetting sysSet = new SysSetting();
            if (UserID == 0 || list == null || list.Count <= 0)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数有误,请检查参数";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            else
            {
                bool _result = _NewInstance.bindPFTLabelLocation(UserID, list);
                if (_result)
                {
                    return ResultsExtensions.GetSuccessJson(_result);
                }
                else
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "提交失败，请联系管理员";
                    return ResultsExtensions.GetFailJson(sysSet);
                }
            }
        }
        #endregion


    }
}
